<?php

declare(strict_types=1);

namespace app\backend\controller\sys;

use app\common\controller\Basic;
use app\common\service\sys\DevLogService;

/**
 * 开发日志 控制器
 * @author kaige
 * @update  2023-09-16
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class DevLog extends Basic
{

  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new DevLogService();
  }

  /**
   * 列表
   */
  public function index()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      return json($this->service->_list($data));
    }
    return _jerr(1000);
  }

  /**
   * 添加
   */
  public function add()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      $data['user_id'] = $this->user['id'];
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      $data = [
        'einfo' => (new \app\common\service\sys\UserService())->getEmployeeInfo($this->user['id']),
      ];
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 编辑
   */
  public function edit()
  {
    $id = input('id/d', 0);
    if ($id === 0) {
      return _jerr(1022);
    }
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      $data = $this->service->getInfoById($id);
      if (isset($data['start']) && !empty($data['start'])){
        $data['start'] = time2str($data['start'],2);
      }
      if (isset($data['end']) && !empty($data['end'])){
        $data['end'] = time2str($data['end'],2);
      }
      $data['einfo'] = (new \app\common\service\sys\UserService())->getEmployeeInfo($data['user_id']);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 添加编辑-表单数据处理
   */
  protected function _form_filter(&$data)
  {
    url_decode($data);
    if (isset($data['start']) && !empty($data['start'])){
      $data['start'] = strtotime($data['start']);
    }
    if (isset($data['end']) && !empty($data['end'])){
      $data['end'] = strtotime($data['end']);
    }
  }

  /**
   * 删除
   */
  public function del()
  {
    if ($this->request->isPost()) {
      $id = input('id', '');
      if ($id === 0) {
        return _jerr(1002);
      }
      return json($this->service->_del($id));
    }
    return _jerr(1000);
  }
}
